package dragon.modules.system.org.intercept;

import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import dragon.core.db.DML;
import dragon.core.exception.ExceptionHandler;
import dragon.core.system.dao.OrgDao;
import dragon.core.system.po.SysOrg;
import dragon.core.system.po.SysUser;
import dragon.core.util.StringUtils;
import dragon.model.bizObject.BizObject;
import dragon.model.intercept.Interceptor;
import dragon.modules.system.org.dao.OrgDaoImpl;
import ejp.Database;
import ejw.ServerInterface;

/**
 * 实现功能：ORG截器类
 */
public class OrgInterceptor extends Interceptor {

	@Override
	public int beforeAdd(BizObject bizObject, Map<String, String> parameterMap, ServerInterface sif, Database db) throws ExceptionHandler {
		if (StringUtils.isEmpty(parameterMap.get("PID"))) {
			parameterMap.put("PID", "0");
			parameterMap.put("DEPTH", "1");
			parameterMap.put("IDPATH", "/" + parameterMap.get("ID") + "/");
			parameterMap.put("CODEPATH", "/" + parameterMap.get("CODE") + "/");
			parameterMap.put("NAMEPATH", "/" + parameterMap.get("NAME") + "/");
		} else {
			OrgDao od = new OrgDaoImpl();
			SysOrg parent = od.getOrg(db, Integer.parseInt(parameterMap.get("PID")));

			parameterMap.put("DEPTH", String.valueOf(parent.getDepth() + 1));
			parameterMap.put("CODEPATH", parent.getCodepath() + parameterMap.get("CODE") + "/");
			parameterMap.put("NAMEPATH", parent.getNamepath() + parameterMap.get("NAME") + "/");
			parameterMap.put("IDPATH", parent.getIdpath() + parameterMap.get("ID") + "/");

			parent = null;
			od = null;
		}
		return INCLUDE_ALL;
	}

	@Override
	public int beforeMod(BizObject bizObject, Map<String, String> parameterMap, ServerInterface sif, Database db) throws ExceptionHandler {
		OrgDao od = new OrgDaoImpl();
		SysOrg org = od.getOrg(db, Integer.parseInt(parameterMap.get("ID")));
		if (!org.getCode().equals(parameterMap.get("CODE"))) {
			// 更新编码路径
			od.codePathRepair(db, org.getId(), org.getCode(), parameterMap.get("CODE"));
		}
		if (!org.getName().equals(parameterMap.get("NAME"))) {
			// 更新名称路径
			od.namePathRepair(db, org.getId(), org.getName(), parameterMap.get("NAME"));
		}
		org = null;
		od = null;
		return INCLUDE_ALL;
	}

	@Override
	public int beforeDel(BizObject bizObject, Map<String, String> parameterMap, ServerInterface sif, Database db) throws ExceptionHandler {
		try {
			JSONArray rows = (JSONArray) new JSONParser().parse(parameterMap.get("rows"));
			JSONObject jsonObj = null;
			for (Object obj : rows) {
				jsonObj = (JSONObject) obj;
				String id = jsonObj.get("ID").toString();
				// 修改涉及到的用户
				DML.executeUpdate("update " + SysUser.tableName + " set Org=0,ougroup='/' where org in(select id from " + SysOrg.tableName + " where id=" + id + " or idpath like '%/" + id + "/%')");
				// 删除部门(包含其子级节点数据)
				DML.executeUpdate("delete from " + SysOrg.tableName + " where id=" + id + " or idpath like '%/" + id + "/%'");
			}
			jsonObj = null;
			rows = null;
		} catch (ParseException e) {
			throw new ExceptionHandler(e);
		}
		return EXCLUDE_ALL;
	}

}
